package com.kye.express.business.help.search;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemHelpSearchLabelBinding;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * 帮助-搜索 ItemViewHolder
 * <p>
 * Created by SkyYu on 2018/1/9
 */
public class HelpSearchLabelItemViewBinder extends ItemViewBinder<String, HelpSearchLabelItemViewBinder.ViewHolder> {

    private OnItemListener itemListener;

    HelpSearchLabelItemViewBinder(OnItemListener itemListener) {
        this.itemListener = itemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_help_search_label, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull String item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemHelpSearchLabelBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }

        public void bindData(final String item) {
            binding.setData(item);
            RxView.clicks(binding.keyword)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (itemListener != null) {
                                itemListener.onItemClicked(item);
                            }
                        }
                    });
        }
    }

    public interface OnItemListener {
        void onItemClicked(String item);
    }
}
